package com.ztesoft.ossup.report.web.config;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.ztesoft.ossup.report.domain.Report;
import com.ztesoft.ossup.report.service.ConditionEnumService;
import com.ztesoft.ossup.report.web.BaseController;
import com.ztesoft.ossup.report.web.BaseController.ResponseEntry;

@Controller
@RequestMapping("/config/condition")
public class ConditionEnumController extends BaseController {
	Logger log = Logger.getLogger(ConditionEnumController.class);

	@Autowired
	ConditionEnumService conditionEnumService;
	
	@RequestMapping(value="" , method = RequestMethod.GET )
	public String toConditionEnum(){
		return "/config/conditionEnum";
	}
	
	@RequestMapping(value="/items",method = RequestMethod.POST)
	@ResponseBody
	public List<Map<String, Object>> queryConditonItems(){
		List<Map<String, Object>> retMap = conditionEnumService.getItems();
		return retMap;
	}
	
//	@RequestMapping(value="/groups",method = RequestMethod.POST)
//	@ResponseBody
//	public List<Map<String, Object>> queryConditionGroups(@RequestBody Map<String, Object> map ){
//		List<Map<String, Object>> retMap = conditionEnumService.getGroup();
//		return retMap;
//	}
	
	@RequestMapping(value = "/addOrEditConditionEnum", method = RequestMethod.POST)
	@ResponseBody
	public String addOrEditGroups(@RequestBody Map<String, Object> conditionEnum) {
		boolean result=false;
		try {
			result = conditionEnumService.addOrEditConditionEnum(conditionEnum);
		} catch (Exception e) {
			e.printStackTrace();
			result=false;
		}
		ResponseEntry responseEntry = new ResponseEntry(SUCCESS);
		if (!result) {
			responseEntry.setCode(FAIL);
		}
		return responseEntry.getJSON();
	}
	
//	@RequestMapping(value = "/addOrEditItems", method = RequestMethod.POST)
//	@ResponseBody
//	public String addOrEditItems(@RequestBody List<Map<String, Object>> itemList) {
//		boolean result=false;
//		try {
//			result = conditionEnumService.addOrEditItem(itemList);
//		} catch (Exception e) {
//			e.printStackTrace();
//			result=false;
//		}
//		ResponseEntry responseEntry = new ResponseEntry(SUCCESS);
//		if (!result) {
//			responseEntry.setCode(FAIL);
//		}
//		return responseEntry.getJSON();
//	}
	
	@RequestMapping(value = "/deleteConditonEnum", method = RequestMethod.POST)
	@ResponseBody
	public String deleteItems(@RequestBody String ids) {
		boolean result=false;
		try {
			result = conditionEnumService.deleteItems(ids);
		} catch (Exception e) {
			e.printStackTrace();
			result=false;
		}
		ResponseEntry responseEntry = new ResponseEntry(SUCCESS);
		if (!result) {
			responseEntry.setCode(FAIL);
		}
		return responseEntry.getJSON();
	}
	
	@RequestMapping(value = "/deleteGroups", method = RequestMethod.POST)
	@ResponseBody
	public String deleteGroups(@RequestBody String groupIds) {
		boolean result=false;
		try {
			result = conditionEnumService.deleteGroups(groupIds);
		} catch (Exception e) {
			e.printStackTrace();
			result=false;
		}
		ResponseEntry responseEntry = new ResponseEntry(SUCCESS);
		if (!result) {
			responseEntry.setCode(FAIL);
		}
		return responseEntry.getJSON();
	}
}
